//package com.jackrain.nea.wms.mq;
//
//import com.aliyun.openservices.ons.api.MessageListener;
//import com.aliyun.openservices.ons.api.PropertyKeyConst;
//import com.aliyun.openservices.ons.api.bean.ConsumerBean;
//import com.aliyun.openservices.ons.api.bean.ProducerBean;
//import com.aliyun.openservices.ons.api.bean.Subscription;
//import com.jackrain.nea.config.PropertiesConf;
//import com.jackrain.nea.mq.MQStartupRunner;
//import com.jackrain.nea.util.ApplicationContextHandle;
//import com.jackrain.nea.util.InstanceUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.cglib.proxy.Callback;
//import org.springframework.cglib.proxy.Enhancer;
//import org.springframework.core.env.Environment;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * Created by ZC on 2017/4/11.
// */
//@Component
//public class StartupRunner implements CommandLineRunner {
//    private static final Logger log = LoggerFactory.getLogger(MQStartupRunner.class);
//    @Autowired
//    private PropertiesConf pconf;
//    @Autowired
//    Environment env;
//    private Map<Subscription, MessageListener> map = new HashMap();
//
//    public StartupRunner() {
//    }
//
//    @Override
//    public void run(String... args) throws Exception {
//
//        Boolean producer = Boolean.valueOf(this.pconf.getProperty("ons.mq.producer.start"));
//        Boolean consumer = Boolean.valueOf(this.pconf.getProperty("ons.mq.consumer.start"));
//
//        if (producer.booleanValue()) {
//            ProducerBean producerBean = (ProducerBean) ApplicationContextHandle.getBean(ProducerBean.class);
//            producerBean.start();
//        }
//
//        if (consumer.booleanValue()) {
//            this.initSubscription();
//            ConsumerBean consumerBean = (ConsumerBean) ApplicationContextHandle.getBean("consumerBean");
//            consumerBean.setSubscriptionTable(this.map);
//            //配置对应 Consumer ID 的最大消息重试次数为2 次
////            consumerBean.getProperties().put(PropertyKeyConst.MaxReconsumeTimes,"4");
//            consumerBean.start();
//        }
//
//    }
//
//    private void initSubscription() throws IllegalAccessException, InstantiationException {
//        String dsPrefixs = this.pconf.getProperty("ons.mq.index");
//        String[] var3 = dsPrefixs.split(",");
//        int var4 = var3.length;
//
//        for (int var5 = 0; var5 < var4; ++var5) {
//            String dsPrefix = var3[var5];
//            String logIntercept = this.pconf.getProperty("ons.mq.consumer.logIntercept" + dsPrefix);
//            String listenerC = this.pconf.getProperty("ons.mq.listenerC" + dsPrefix);
//            String topic = this.pconf.getProperty("ons.mq.topic" + dsPrefix);
//            String expr = this.pconf.getProperty("ons.mq.expr" + dsPrefix, "");
//            if (listenerC == null) {
//                log.error("注意：Topic" + topic + "无监听程序");
//                return;
//            }
//
//            log.debug("topic:" + topic + "expr:" + expr);
//
//            Class classz = InstanceUtil.getClass(listenerC);
//            if (logIntercept != null) {
//                Class logAspect = InstanceUtil.getClass(logIntercept);
//                Enhancer enhancer = new Enhancer();
//                enhancer.setSuperclass(classz);
//                enhancer.setCallback((Callback) logAspect.newInstance());
//                Object o = enhancer.create();
//                if (o != null && o instanceof MessageListener) {
//                    MessageListener messageListener = (MessageListener) o;
//                    Subscription subscription = new Subscription();
//                    subscription.setTopic(topic);
//                    subscription.setExpression(expr);
//                    this.map.put(subscription, messageListener);
//                } else {
//                    log.error("MQ普通消息消费监听程序类型错误，参数ons.mq.topic" + dsPrefix);
//                }
//            } else {
//                MessageListener messageListener = (MessageListener) classz.newInstance();
//                Subscription subscription = new Subscription();
//                subscription.setTopic(topic);
//                subscription.setExpression(expr);
//                this.map.put(subscription, messageListener);
//            }
//        }
//
//    }
//
//
//
//}
